﻿using Repid.Entity.Basic;
using Repid.MvcExtension.Controllers;
using Repid.Core.Aop;
using Repid.Utility.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Repid.MvcApplication.Areas.Manage.Controllers
{
    public class DepartmentController : ManageController
    {
        public ActionResult List()
        {
            return View();
        }

        public ActionResult Page(string id, string pid)
        {
            BasicDepartment model;

            if (string.IsNullOrEmpty(id))
            {
                model = new BasicDepartment();

                model.Pid = pid;
            }
            else { model = ServiceFactory.BasicDepartment.Get(id); }

            return View(model);
        }

        public string Grid()
        {
            return JsonHelper.Serialize(ServiceFactory.BasicDepartment.GetAll());
        }

        public string Tree()
        {
            var list = ServiceFactory.BasicDepartment.GetAll();

            var newlist = list.Select(m => new
            {
                id = m.ID,
                text = m.Name,
                pid = m.Pid
            }).ToList();

            newlist.Insert(0, new
            {
                id = "0",
                text = "无父级",
                pid = string.Empty
            });

            return JsonHelper.Serialize(newlist);
        }

        public string BaseTree()
        {
            var list = ServiceFactory.BasicDepartment.GetAll();

            var newlist = list.Select(m => new
            {
                id = m.ID,
                text = m.Name,
                pid = m.Pid
            }).ToList();

            return JsonHelper.Serialize(newlist);
        }

        public string Save(BasicDepartment model)
        {
            return ServiceFactory.BasicDepartment.Save(model).Serialize();
        }

        public string Remove(string id)
        {
            return ServiceFactory.BasicDepartment.Delete(id).Serialize();
        }

        public string SetOrder(string id, string order, string pid)
        {
            return ServiceFactory.BasicDepartment.Order(id, order, pid).Serialize();
        }
    }
}
